<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{ layer.layer_info.name }} - Layer Documentation</title>
    <style>
        body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; max-width: 1200px; margin: 0 auto; padding: 20px; line-height: 1.6; }
        .header { border-bottom: 2px solid #eee; padding-bottom: 20px; margin-bottom: 30px; }
        .section { margin-bottom: 30px; }
        .badge { display: inline-block; background: #007acc; color: white; padding: 2px 8px; border-radius: 3px; font-size: 12px; margin-right: 10px; }
        .policy-immediate { background: #28a745; color: white; text-decoration: none; }
        .policy-lazy { background: #ffc107; color: #212529; text-decoration: none; }
        .policy-force { background: #dc3545; color: white; text-decoration: none; }
        .policy-skip { background: #6c757d; color: white; text-decoration: none; }
        .policy-immediate:hover { background: #1e7e34; }
        .policy-lazy:hover { background: #e0a800; }
        .policy-force:hover { background: #c82333; }
        .policy-skip:hover { background: #545b62; }
        table { width: 100%; border-collapse: collapse; margin-top: 10px; table-layout: auto; }
        th, td { text-align: left; padding: 8px 12px; border-bottom: 1px solid #ddd; }
        th { background: #f8f9fa; font-weight: 600; }
        td:nth-child(3) {
            width: auto;
        }
        code { background: #f1f3f4; padding: 2px 4px; border-radius: 3px; font-family: 'Monaco', monospace; font-size: 14px; }
        code.long-default { word-break: break-all; white-space: pre-wrap; display: block; min-width: 30ch; }
        .back-link { margin-bottom: 20px; }
        .back-link a { text-decoration: none; color: #007acc; }
        .deps { display: flex; flex-wrap: wrap; gap: 5px; }
        .dep-badge { background: #28a745; color: white; padding: 2px 6px; border-radius: 3px; font-size: 11px; text-decoration: none; }
        .dep-badge:hover { background: #1e7e34; }
        /* Main content headers styling */
        h1, h2, h3, h4, h5, h6 { color: #333; margin-top: 20px; margin-bottom: 10px; }
        /* Companion documentation content styling */
        .companion-content h1, .companion-content h2, .companion-content h3, .companion-content h4, .companion-content h5, .companion-content h6 { color: #333; margin-top: 20px; margin-bottom: 10px; }
        .companion-content p { margin: 10px 0; }
        .companion-content ul, .companion-content ol { margin: 10px 0; padding-left: 30px; }
        .companion-content blockquote { border-left: 4px solid #007acc; padding-left: 15px; margin: 15px 0; color: #666; background: #f8f9fa; padding: 10px 15px; }
        .companion-content pre { background: #f8f9fa; padding: 15px; border-radius: 5px; overflow-x: auto; border-left: 4px solid #007acc; }
        .companion-content table { border: 1px solid #ddd; }
        .companion-content th, .companion-content td { border: 1px solid #ddd; }

        /* AsciiDoc admonition blocks (NOTE, TIP, WARNING, etc.) */
        .admonitionblock {
            margin: 1.5em 0;
            padding: 0.4em 0.6em;
            border-left: 4px solid;
            background: #f8f9fa;
            border-radius: 0 4px 4px 0;
        }

        .admonitionblock .title {
            font-weight: bold;
            text-transform: uppercase;
            font-size: 0.85em;
            margin-bottom: 0.25em;
            letter-spacing: 0.5px;
        }

        .admonitionblock .content {
            margin: 0;
        }

        /* Reduce spacing for paragraphs inside admonitions */
        .admonitionblock p {
            margin: 0.25em 0;
        }

        .admonitionblock p:first-child {
            margin-top: 0;
        }

        .admonitionblock p:last-child {
            margin-bottom: 0;
        }

        /* Specific admonition types */
        .admonitionblock.note {
            border-color: #17a2b8;
            background: #d1ecf1;
        }

        .admonitionblock.note .title {
            color: #0c5460;
        }

        .admonitionblock.tip {
            border-color: #28a745;
            background: #d4edda;
        }

        .admonitionblock.tip .title {
            color: #155724;
        }

        .admonitionblock.important {
            border-color: #ffc107;
            background: #fff3cd;
        }

        .admonitionblock.important .title {
            color: #856404;
        }

        .admonitionblock.warning,
        .admonitionblock.caution {
            border-color: #dc3545;
            background: #f8d7da;
        }

        .admonitionblock.warning .title,
        .admonitionblock.caution .title {
            color: #721c24;
        }
    </style>
</head>
<body>
    <div class="back-link">
        <a href="index.html">← Back to Layer Index</a>
    </div>

    <div class="header">
        <h1>{{ layer.layer_info.name }}</h1>
        {% if layer.layer_info.category %}<span class="badge">{{ layer.layer_info.category }}</span>{% endif %}
        {% if layer.layer_info.version %}<span class="badge">v{{ layer.layer_info.version }}</span>{% endif %}
        <p>{{ layer.layer_info.description or 'No description available' }}</p>
    </div>

    {% if layer.companion_html %}
    <div class="section">
        <h2>Additional Documentation</h2>
        <div class="companion-content">
            {{ layer.companion_html|safe }}
        </div>
    </div>
    {% endif %}

    {% if layer.dependencies.static_dep or layer.dependencies.dyn_dep or layer.reverse_dependencies or layer.layer_info.provides or layer.layer_info.provider_requires %}
    <div class="section">
        <h2>Relationships</h2>
        {% if layer.dependencies.static_dep or layer.dependencies.dyn_dep %}
        <p><strong>Depends on:</strong></p>
        <div class="deps">
            {%- for dep in layer.dependencies.static_dep %}
            <a href="{{ dep }}.html" class="dep-badge">{{ dep }}</a>
            {%- endfor %}
            {%- for dep in layer.dependencies.dyn_dep %}
            <span class="dep-badge" style="background: #6c757d; cursor: default;" title="Dynamic dependency (contains environment variables)">{{ dep }}</span>
            {%- endfor %}
        </div>
        {% endif %}
        {% if layer.reverse_dependencies %}
        <p><strong>Required by:</strong></p>
        <div class="deps">
            {% for dep in layer.reverse_dependencies %}
            <a href="{{ dep }}.html" class="dep-badge">{{ dep }}</a>
            {% endfor %}
        </div>
        {% endif %}
        {% if layer.layer_info.provides %}
        <p><strong>Provides:</strong> {{ layer.layer_info.provides|join(', ') }}</p>
        {% endif %}
        {% if layer.layer_info.provider_requires %}
        <p><strong>Requires Provider:</strong> {{ layer.layer_info.provider_requires|join(', ') }}</p>
        {% endif %}
    </div>
    {% endif %}

    {% if layer.variables or layer.required_variables %}
    <div class="section">
        <h2>Configuration Variables</h2>
        {% if layer.required_variables %}
        <p><strong>References:</strong>
        {% for var_ref in layer.required_variables %}
        <code>{{ var_ref }}</code>{% if not loop.last %}, {% endif %}
        {% endfor %}
        </p>
        {% endif %}
        {% if layer.variables %}
        <p><strong>Declares</strong>{% if layer.variable_prefix %} (prefix: <code>{{ layer.variable_prefix }}</code>){% endif %}:</p>
        <table>
            <thead>
                <tr>
                    <th>Variable</th>
                    <th>Description</th>
                    <th>Default</th>
                    <th>Validation</th>
                    <th>Policy</th>
                </tr>
            </thead>
            <tbody>
                {% for var_name, var in layer.variables.items() %}
                <tr>
                    <td><code>{{ var.name }}</code></td>
                    <td>{{ var.description or 'No description' }}</td>
                    <td>
                       {% if var.original_value %}
                           {% if var.original_value|length > 25 %}
                           <code class="long-default">{{ var.original_value }}</code>
                           {% else %}
                           <code>{{ var.original_value }}</code>
                           {% endif %}
                       {% elif var.set_policy == 'skip' %}
                           <code>&lt;disabled&gt;</code>
                       {% elif var.validation_description and 'empty' in var.validation_description.lower() %}
                           <code>&lt;empty&gt;</code>
                       {% else %}
                           <code>&lt;not set&gt;</code>
                       {% endif %}
                    </td>
                    <td>{{ var.validation_description or 'None' }}</td>
                    <td>
                        <a href="variable-validation.html#set-policies" class="badge policy-{{ var.set_policy }}" title="Click for policy and validation help">{{ var.set_policy }}</a>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
        {% endif %}
    </div>
    {% endif %}

    {% if (
       layer.mmdebstrap.get('packages') or
       layer.mmdebstrap.get('mirrors') or
       layer.mmdebstrap.get('suite') or
       layer.mmdebstrap.get('variant') or
       layer.mmdebstrap.get('architectures') or
       layer.mmdebstrap.get('dpkgopts') or
       layer.mmdebstrap.get('aptopts')
       ) %}
    <div class="section">
        <h2>mmdebstrap</h2>

        {% if layer.mmdebstrap.get('suite') %}
        {% set suite = layer.mmdebstrap.suite %}
        <h3>Suite</h3>
        {% if suite is string %}
        <ul>
            <li><code>{{ suite }}</code></li>
        </ul>
        {% else %}
        <ul>
            {% for s in suite %}
            <li><code>{{ s }}</code></li>
            {% endfor %}
        </ul>
        {% endif %}
        {% endif %}

        {% if layer.mmdebstrap.get('variant') %}
        {% set variant = layer.mmdebstrap.variant %}
        <h3>Variant</h3>
        <p>Installs package set:</p>
        {% if variant is string %}
        <ul>
            <li><code>{{ variant }}</code></li>
        </ul>
        {% else %}
        <ul>
            {% for v in variant %}
            <li><code>{{ v }}</code></li>
            {% endfor %}
        </ul>
        {% endif %}
        {% endif %}

        {% if layer.mmdebstrap.get('architectures') %}
        {% set architectures = layer.mmdebstrap.architectures %}
        <h3>Architectures</h3>
        {% if architectures is string %}
        <ul>
            <li><code>{{ architectures }}</code></li>
        </ul>
        {% else %}
        <ul>
            {% for arch in architectures %}
            <li><code>{{ arch }}</code></li>
            {% endfor %}
        </ul>
        {% endif %}
        {% endif %}

        {% if layer.mmdebstrap.get('packages') %}
        {% set packages = layer.mmdebstrap.packages %}
        <h3>Packages</h3>
        <p>Installs:</p>
        <ul>
            {% for package in packages %}
            <li><code>{{ package }}</code></li>
            {% endfor %}
        </ul>
        {% endif %}

        {% if layer.mmdebstrap.get('mirrors') %}
        {% set mirrors = layer.mmdebstrap.mirrors %}
        <h3>Mirrors</h3>
        <ul>
            {% for mirror in mirrors %}
            <li><code>{{ mirror }}</code></li>
            {% endfor %}
        </ul>
        {% endif %}

        {% if layer.mmdebstrap.get('dpkgopts') %}
        {% set dpkgopts = layer.mmdebstrap.dpkgopts %}
        <h3>dpkgopts</h3>
        <ul>
            {% for opt in dpkgopts %}
            <li><code>{{ opt }}</code></li>
            {% endfor %}
        </ul>
        {% endif %}

        {% if layer.mmdebstrap.get('aptopts') %}
        {% set aptopts = layer.mmdebstrap.aptopts %}
        <h3>aptopts</h3>
        <ul>
            {% for opt in aptopts %}
            <li><code>{{ opt }}</code></li>
            {% endfor %}
        </ul>
        {% endif %}
    </div>
    {% endif %}

    {%- if layer.env %}
    <div class="section">
        <h2>Environment</h2>
        <p>Sets the following variables:</p>
        <table>
            <thead>
                <tr>
                    <th>Variable</th>
                    <th>Value</th>
                </tr>
            </thead>
            <tbody>
                {% for env_var, env_value in layer.env.items() %}
                <tr>
                    <td><code>{{ env_var }}</code></td>
                    <td><code>{{ env_value }}</code></td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
    {%- endif %}

    <div class="section">
        <h2>Attributes</h2>
        <p><strong>File:</strong> <code>{{ layer.file_path }}</code></p>
        {% if layer.layer_info.conflicts %}
        <p><strong>Conflicts:</strong> {{ layer.layer_info.conflicts|join(', ') }}</p>
        {% endif %}
    </div>
</body>
</html>
